package com.sskj.finance.ui.activity;


import android.os.Bundle;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.github.yoojia.inputs.AndroidNextInputs;
import com.github.yoojia.inputs.StaticScheme;
import com.github.yoojia.inputs.WidgetAccess;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.DisposUtil;
import com.sskj.finance.R;
import com.sskj.finance.R2;
import com.sskj.finance.bean.HomeLendBlend;
import com.sskj.finance.bean.HomeLendLendCoinListBean;
import com.sskj.finance.presenter.IWantFinanceActivityPresenter;
import com.sskj.finance.util.CoinPop;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.util.BottomSheetUtil;
import com.sskj.lib.util.CoinUtil;
import com.sskj.lib.util.SchemeUtil;
import com.sskj.lib.util.TipUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;

@Route(path = RConfig.FINAN_I_WANT_FINANCE)//我要理财
public class IWantFinanceActivity extends BaseActivity<IWantFinanceActivityPresenter> {

    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.llCoinType)
    LinearLayout llCoinType;
    @BindView(R2.id.left_img)
    ImageView leftImg;
    @BindView(R2.id.left_tv)
    TextView leftTv;
    @BindView(R2.id.ivBack)
    LinearLayout ivBack;
    @BindView(R2.id.tvRecord)
    TextView tvRecord;
    @BindView(R2.id.rlTop)
    RelativeLayout rlTop;
    @BindView(R2.id.tvEarnTitle)
    TextView tvEarnTitle;
    @BindView(R2.id.tvEarn)
    TextView tvEarn;
    @BindView(R2.id.tvAccumulateFinanceTitle)
    TextView tvAccumulateFinanceTitle;
    @BindView(R2.id.tvAvailableTitle)
    TextView tvAvailableTitle;
    @BindView(R2.id.tvAccumulateFinance)
    TextView tvAccumulateFinance;
    @BindView(R2.id.tvAvailable)
    TextView tvAvailable;
    @BindView(R2.id.etMoney)
    EditText etMoney;
    @BindView(R2.id.tvFinanceCoinType)
    TextView tvFinanceCoinType;
    @BindView(R2.id.tvDay)
    TextView tvDay;
    @BindView(R2.id.tvRate)
    TextView tvRate;
    @BindView(R2.id.checkbox)
    CheckBox checkbox;
    @BindView(R2.id.btSubmit)
    Button btSubmit;
    @BindView(R2.id.tvSelectDay)
    TextView tvSelectDay;
    private BottomSheetDialog bottomSheet;
    private BottomSheetDialog bottomSheetCoin;
    private List<HomeLendLendCoinListBean> coinListBeans;
    private HomeLendLendCoinListBean coinBean;
    private Disposable dayDispo;
    private Disposable coinDispo;
    private String rate;
    private String days;
    private BottomSheetDialog bottomSheetDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.finan_activity_iwant_finance;
    }

    @Override
    public IWantFinanceActivityPresenter getPresenter() {
        return new IWantFinanceActivityPresenter();
    }

    @Override
    protected void initView() {
        ClickUtil.click(tvRecord, () -> {
            ARouter.getInstance().build(RConfig.FINAN_FINANCE_RECORD).navigation();
        });
        ClickUtil.click(llCoinType, () -> {
            showCoin();
        });
        ClickUtil.click(tvSelectDay, () -> {
            showDay();
        });
        AndroidNextInputs inputs = new AndroidNextInputs();
        WidgetAccess access = new WidgetAccess(this);
        inputs.add(access.findEditText(R.id.etMoney), StaticScheme.notBlank());
        ClickUtil.click(btSubmit, () -> {
            if (inputs.test()) {
                bottomSheetDialog = BottomSheetUtil.showPwd(this, str -> {
                    bottomSheetDialog.dismiss();
                    mPresenter.homeLendLendAdd(coinBean.getPname(), etMoney.getText().toString(),
                            rate, days, checkbox.isChecked() ? "1" : "0", str[0]);
                });
                bottomSheetDialog.show();
            }
        });
    }

    @Override
    protected void initData() {
        mPresenter.homeLendLendCoinList();
    }

    private void showCoin() {
        if (bottomSheetCoin == null) {
            mPresenter.homeLendLendCoinList();
        }
        bottomSheetCoin.show();
    }

    private void showDay() {
        if (bottomSheet == null) {
            mPresenter.homeLendLendCoinList();
        }
        bottomSheet.show();
    }

    public void updateUI(List<HomeLendLendCoinListBean> data) {
        coinListBeans = data;
        if (coinListBeans != null && coinListBeans.size() > 0) {
            coinBean = coinListBeans.get(0);
            updateCoin();
        }

    }

    private void updateCoin() {
        days = coinBean.getDay_rate().get(0).getDays();
        tvDay.setText(days + App.INSTANCE.getString(R.string.finan_borrowActivity4_j));
        rate = coinBean.getDay_rate().get(0).getRate();
        tvRate.setText(rate);
        dayDispo = Flowable.fromIterable(coinBean.getDay_rate())
                .map(dayRateBean -> dayRateBean.getDays()+App.INSTANCE.getString(R.string.finan_borrowActivity4_j))
                .toList()
                .subscribe((strings, throwable) -> {
                    bottomSheet = BottomSheetUtil.getBottomSheet(this, null, (recyclerView, i, view) -> {
                        bottomSheet.dismiss();
                        days = coinBean.getDay_rate().get(i).getDays();
                        tvDay.setText(days + App.INSTANCE.getString(R.string.finan_borrowActivity4_j));
                        rate = coinBean.getDay_rate().get(i).getRate();
                        tvRate.setText(rate);

                    }, strings);
                });
        setTitle(String.format(App.INSTANCE.getString(R.string.finan_iWantFinanceActivity2_j), coinBean.getPname()));
        tvEarnTitle.setText(String.format(App.INSTANCE.getString(R.string.finan_iWantFinanceActivity3_j), coinBean.getPname()));
        tvAccumulateFinanceTitle.setText(String.format(App.INSTANCE.getString(R.string.finan_iWantFinanceActivity4_j), coinBean.getPname()));
        tvAvailableTitle.setText(String.format(App.INSTANCE.getString(R.string.finan_iWantFinanceActivity5_j), coinBean.getPname()));
        tvFinanceCoinType.setText(coinBean.getPname());
        coinDispo = Flowable.fromIterable(coinListBeans)
                .map(HomeLendLendCoinListBean::getPname)
                .toList()
                .subscribe((strings, throwable) -> {
                    bottomSheetCoin = BottomSheetUtil.getBottomSheet(this, null, (recyclerView, i, view) -> {
                        bottomSheetCoin.dismiss();
                        coinBean = coinListBeans.get(i);
                        updateCoin();
                    }, strings);
                });
        mPresenter.homeLendBlend(coinBean.getPname());
    }

    @Override
    protected void onDestroy() {
        DisposUtil.close(dayDispo);
        DisposUtil.close(coinDispo);
        super.onDestroy();
    }

    public void addSuccess() {
        ARouter.getInstance().build(RConfig.FINAN_FINANCE_RECORD).navigation();

        finish();
    }

    public void updateHead(HomeLendBlend data) {
        tvEarn.setText(CoinUtil.keepCoinNumMax(data.getPname(),data.getIn_come()));
        tvAccumulateFinance.setText(CoinUtil.keepCoinNumMax(data.getPname(),data.getLend_sum()));
        tvAvailable.setText(CoinUtil.keepCoinNumMax(data.getPname(),data.getUsable()));
    }
}
